package com.perfect.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.perfect.base.BaseService;
import com.perfect.dao.ProductMapper;
import com.perfect.exceptions.NoLoginException;
import com.perfect.exceptions.ParamsException;
import com.perfect.model.DiscountReport;
import com.perfect.model.ProductCountModel;
import com.perfect.query.ProductPlusQuery;
import com.perfect.query.ProductQuery;
import com.perfect.utils.AssertUtil;
import com.perfect.vo.Product;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ProductService extends BaseService<Product,Integer> {

    @Resource
    private ProductMapper productMapper;

    //查询平台商品

    public Map<String, Object> selectByParams(ProductQuery productQuery) {

        //System.out.println( productMapper.selectByParams(productQuery));

        Map<String, Object> map = new HashMap<>();
        // 开启分页
        PageHelper.startPage(productQuery.getPage(), productQuery.getLimit());
        // 得到对应分页对象
        PageInfo<Product> pageInfo = new PageInfo<>(productMapper.selectByParams(productQuery));
// 设置map对象
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", pageInfo.getTotal());
// 设置分页好的列表
        map.put("data", pageInfo.getList());

        return map;

    }


//查询待审核列表
    public Map<String, Object> selectByParams(ProductPlusQuery productPlusQuery) {



        Map<String, Object> map = new HashMap<>();
        // 开启分页
        PageHelper.startPage(productPlusQuery.getPage(), productPlusQuery.getLimit());
        // 得到对应分页对象
        PageInfo<Product> pageInfo = new PageInfo<>(productMapper.selectProductPlusByParams(productPlusQuery));
// 设置map对象
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", pageInfo.getTotal());
// 设置分页好的列表
        map.put("data", pageInfo.getList());

        return map;

    }



//拒绝审核
    public Integer deleteProductPlusBatch(Integer[] ids) throws DataAccessException {
        return productMapper.deleteProductPlusBatch(ids);
    }


    @Transactional(propagation = Propagation.REQUIRED)
    //通过审核
    public void updateProductPlus(Integer proId) throws DataAccessException {

        AssertUtil.isTrue(productMapper.updateProductPlus(proId) != 1, "审核通过失败！");

    }


    //添加商品
    @Transactional(propagation = Propagation.REQUIRED)
    public void addProduct(Product product,String desc) {
        /* 1. 校验参数 */
        checkSaleChanceParams(product.getProName(),product.getShopName(),
                String.valueOf(product.getProPrice()),String.valueOf(product.getProClassid()),
                String.valueOf(product.getProVolume()),String.valueOf(product.getProNumber()));

        AssertUtil.isTrue (!(product.getProPrice() instanceof Float),"请输入数字");
        AssertUtil.isTrue(!(product.getProVolume() instanceof Integer),"请输入数字");
        AssertUtil.isTrue(!(product.getProNumber() instanceof Integer),"请输入数字");

        product.setProState(0);
        product.setAuditState(2);


// 3. 执行添加操作，判断受影响的行数
        AssertUtil.isTrue(productMapper.insertSelective(product) != 1, "添加商品失败！");


    }


//工具，参数非空判断
    private void checkSaleChanceParams(String proName,String shopName,
    String proPrice,String proClassid,
      String proVolume,String proNumber) {

        AssertUtil.isTrue(StringUtils.isBlank(proName), "商品名称不能为空！");

        AssertUtil.isTrue(StringUtils.isBlank(shopName), "店铺不能为空");

        AssertUtil.isTrue(StringUtils.isBlank(proPrice), "商品价格不能为空！");

        AssertUtil.isTrue(StringUtils.isBlank(proClassid), "商品类型不能为空！");

        AssertUtil.isTrue(StringUtils.isBlank(proVolume), "商品销量不能为空！");

        AssertUtil.isTrue(StringUtils.isBlank(proNumber), "商品库存不能为空！");


    }


    public List<Product> listProducts() {

        List<Product> list = productMapper.listProducts();
        AssertUtil.isTrue(list.size()<=0,"暂无数据");

        return list;
    }

    /**
     * 查询不同种类的优惠券总数
     * @return
     */
    public Map<String,Object> listProductCount(){
        List<String> listNameP = new ArrayList<>();
        List<Integer> listCountP = new ArrayList<>();
        List<ProductCountModel> list = new ArrayList<>();
        list = productMapper.selectProductCount();
        for (ProductCountModel item: list){
            listNameP.add(item.getProductName());
            listCountP.add(item.getCount());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("listNameP",listNameP);
        map.put("listCountP",listCountP);
        return map;
    }
}
